package org.jash.shop;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.jash.shop.entity.LoginEntity;

import java.util.HashMap;

public class RegisterActivity extends AppCompatActivity {
    private EditText username, password, confirmPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        confirmPassword = findViewById(R.id.confirm_password);
        findViewById(R.id.register).setOnClickListener(v -> {
            String un = username.getText().toString();
            String pw = password.getText().toString();
            String cp = confirmPassword.getText().toString();
            if (!pw.equals(cp)) {
                Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            HttpUtils.doPost("http://10.161.52.111:8080/user/register", new HashMap<String, String>() {{
                put("username", un);
                put("password", pw);
                put("repassword", pw);
            }}, LoginEntity.class,
                    r -> {
                        if (r.getCode() == 200) {
                            setResult(RESULT_OK, new Intent().putExtra("username", un).putExtra("password", pw));
                            finish();
                        }
                    },
                    t -> {
                        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
                    });
        });
        findViewById(R.id.goto_login).setOnClickListener(v -> finish());

    }
}